package com.leyou.controller;

import com.leyou.api.ActivityService;
import com.leyou.constant.DefineException;
import com.leyou.constant.ResponseMessage;
import com.leyou.constant.Result;
import com.leyou.domain.db1entity.Couponactivity;
import com.leyou.domain.vo.CouponActivityPutVO;
import com.leyou.domain.vo.CouponActivityUpdateVO;
import com.leyou.domain.vo.PageVO;
import com.leyou.utils.Utils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@SuppressWarnings("all")
@RequestMapping("activity")
@RestController
public class ActivityController {


    @Autowired
    ActivityService activityService;


    @PostMapping("putActivity")
    public ResponseMessage putActivity(@RequestBody CouponActivityPutVO couponActivityPutVO, @CookieValue(name = "ticket") String ticket) {

        //空值验证
        if (!Utils.isNull(couponActivityPutVO.getBackgroundcolor(),
                couponActivityPutVO.getBackgroundurl(),
                couponActivityPutVO.getReceivebtnurl(),
                couponActivityPutVO.getSharebtnurl(),
                couponActivityPutVO.getShareurl(),
                couponActivityPutVO.getTheme()) &&
                !Utils.isNull(couponActivityPutVO.getBegintime(), couponActivityPutVO.getEndtime())
        ) {
            //判断时间有效性
            //结束时间必须晚于开始时间
            if (couponActivityPutVO.getBegintime().compareTo(couponActivityPutVO.getEndtime()) > 0) {
                return Result.error(400000, "活动结束时间应大于开始时间");
            } else {
                //固定结束时间大于活动结束时间
                if (couponActivityPutVO.getDeadline() != null) {
                    if (couponActivityPutVO.getDeadline().compareTo(couponActivityPutVO.getEndtime()) >= 0) {

                    } else {
                        return Result.error(400000, "优惠券有效期要大于等于活动结束时间");
                    }
                } else {
                    if (couponActivityPutVO.getDuration() == null) {
                        return Result.error(400000, "固定结束时间和固定持续天数都为空");
                    }
                }
            }

        } else {
            return Result.error();
        }


        //验证优惠券列表
        if (couponActivityPutVO.getCouponsKeyList() == null || couponActivityPutVO.getCouponsKeyList().size() == 0) {
            //参数为空
            return Result.error(400003, "优惠券列表为空");
        } else {
            //验证优惠券有效性
            try {
                activityService.checkCoupon(couponActivityPutVO.getCouponsKeyList());
            } catch (DefineException e) {
                return Result.success(e.getErrorCode(), e.getMessage());
            }
        }

        //添加
        try {
            couponActivityPutVO.setManagerkey(activityService.getManagerByTicket(ticket).getManagername());
            activityService.putCouponActivity(couponActivityPutVO);
        } catch (DefineException e) {
            return Result.error(e.getErrorCode(), e.getMessage());
        }

        return Result.success(200002, "添加活动成功");

    }


    @PostMapping("getActivityList")
    public ResponseMessage getActivityList(@RequestParam("page_num") Integer pageNum, @RequestParam("page_size") Integer pageSize) {
        Integer total = activityService.getTotal().size();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        if (pageNum != null && pageSize != null) {
            map.put("activity_list", activityService.getCouponActivityList(pageNum, pageSize));
        } else {
            map.put("activity_list", activityService.getCouponActivityList(1, 5));
        }
        return Result.success(map);
    }


    @PostMapping("getLogList")
    public ResponseMessage getLogList(@RequestParam("page_num") Integer pageNum, @RequestParam("page_size") Integer pageSize) {

        if (!Utils.isNull(pageNum, pageSize)) {
            return Result.success(activityService.getLogList(new PageVO(pageNum, pageSize)));
        } else {
            return Result.success(activityService.getLogList(new PageVO(1, 5)));
        }
    }


    @PostMapping("updateActivity")
    ResponseMessage updateActivity(@RequestBody CouponActivityUpdateVO couponActivityUpdateVO, @CookieValue(name = "ticket") String ticket) {

        String managername = activityService.getManagerByTicket(ticket).getManagername();
        try {
            activityService.updateActivity(couponActivityUpdateVO, managername);
        } catch (DefineException d) {
            return Result.error(d.getErrorCode(), d.getMessage());
        }
        return Result.success(200003, "修改成功");
    }

    @PostMapping("getActivityById")
    ResponseMessage getActivityById(@RequestBody CouponActivityUpdateVO couponActivityUpdateVO) {
        Couponactivity couponactivity;
        if (!StringUtils.isBlank(couponActivityUpdateVO.getActivitykey())) {
            try {
                couponactivity = activityService.getActivityById(couponActivityUpdateVO.getActivitykey());
            } catch (DefineException d) {
                return Result.error(d.getErrorCode(), d.getMessage());
            }

        } else {
            return Result.success(400000, "必要参数为空");
        }
        return Result.success(couponactivity);
    }


    @PostMapping("getActivityUrl")
    ResponseMessage getActivityUrl(@RequestBody CouponActivityUpdateVO couponActivityUpdateVO) {
        if (!StringUtils.isBlank(couponActivityUpdateVO.getActivitykey())) {
            return Result.success(200000, "访问成功", "http://192.168.20.11:8888/home.html?activitykey=" + couponActivityUpdateVO.getActivitykey());
        } else {
            return Result.error(900008, "请求参数为空");
        }
    }


}

